define(function(require, exports){
	var $		= require("jquery");
	var pubjs	= require('pubjs');
	var view	= require('@base/view');

	var NameReplace = view.widget.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'class': 'P-info',
				'id': null,
				'view_model': {
				}
			});

			this.Super('init', arguments);
		},
		afterBuild:function(){
			var el = this.getDOM('body');
			var cls = this.getConfig('class');
			el.addClass(cls);
			el.append($([
				'<div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('关键字：') + '</label>',
						'<div class="group-content">',
							'<span pub-name="originalKeyword", pub-mod="@base/common/input.text" />',
							'<span class="ml10" pub-name="tipOriginalKeyword" pub-mod="@base/tip.desc" />',
						'</div>',
					'</div>',

					'<div class="M-formConGroup">',
						'<label class="group-label">' + LANG('替换为：') + '</label>',
						'<div class="group-content">',
							'<span pub-name="newKeyword", pub-mod="@base/common/input.text" />',
							'<span class="ml10" pub-name="tipNewKeyword" pub-mod="@base/tip.desc" />',
						'</div>',
					'</div>',
				'</div>'
			].join('')));

			this.buildFromTemplate({
				'originalKeyword': {
					'placeholder':  LANG('旧关键字'),
					'width': 300
				},
				'newKeyword': {
					'placeholder':  LANG('新关键字'),
					'width': 300
				},
				'tipOriginalKeyword': {
					'data': LANG('需要替换掉的关键字')
				},
				'tipNewKeyword': {
					'data': LANG('需要替换成的关键字')
				}
			});
		},
		setData: function(data){
			this.$id = data.id;
			return this;
		},
		getData: function(){
			var cs = this.$;
			return {
				'originalKeyword': cs.originalKeyword.getValue() || '',
				'newKeyword': cs.newKeyword.getValue() || ''
			};
		},
		validate: function(data)
		{
			var valid = true;
			if(!data || !data.originalKeyword)
			{
				pubjs.alert(LANG('请填写旧关键字'));
				return false;
			}


			return valid;
		},
		onDialogOk: function(){
			var self = this;
			var data = this.getData();
			if(this.validate(data))
			{
				self.fire('nameReplace',{data: data, id: self.$id});
				self.reset();
			}

			return self;
		},
		onDialogCancel:function(){
			this.reset();
			return this;
		},
		reset: function(){
			this.hide();
			this.vm.reset();
			this.$.newKeyword.setValue('');
			this.$.originalKeyword.setValue('');
			return this;
		}
	});

	exports.base = NameReplace;
});